package com.source.barcodeMachineGrantAndRecovery.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.source.barcodeMachineGrantAndRecovery.VO.BarcodeMachineGrantVO;
import com.source.barcodeMachineGrantAndRecovery.entity.BarcodeMachineGrant;
import com.source.barcodeMachineGrantAndRecovery.mapper.BarcodeMachineGrantMapper;
import com.source.barcodeMachineGrantAndRecovery.service.BarcodeMachineGrantService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.source.utils.RedisUtil;
import com.source.utils.UUIDUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Feng
 * @since 2021-02-24
 */

@Service
public class BarcodeMachineGrantServiceImpl extends ServiceImpl<BarcodeMachineGrantMapper, BarcodeMachineGrant> implements BarcodeMachineGrantService {

    @Resource
    BarcodeMachineGrantMapper barcodeMachineGrantMapper;

    @Autowired
    private RedisUtil redisUtil;

    @Value("${qrcode.stalls.redis.timeout}")
    private int timeout;

    @Override
    public BarcodeMachineGrantVO find(int id) {
        return barcodeMachineGrantMapper.getByGrantId(id);
    }

    @Override
    public List<BarcodeMachineGrantVO> find() {
        return barcodeMachineGrantMapper.listAll();
    }

    @Override
    public Page<BarcodeMachineGrantVO> find(int current, int size) {
        return barcodeMachineGrantMapper.pageAll(new Page<>(current, size));
    }

    @Override
    public List<BarcodeMachineGrantVO> findByBarcodeMachineId(int id) {
        return barcodeMachineGrantMapper.listByBarcodeMachineId(id);
    }

    @Override
    public Page<BarcodeMachineGrantVO> findByBarcodeMachineId(int id, int current, int size) {
        return barcodeMachineGrantMapper.pageByBarcodeMachineId(id, new Page<>(current, size));
    }

    @Override
    public List<BarcodeMachineGrantVO> like(String str) {
        return barcodeMachineGrantMapper.listLike(str);
    }

    @Override
    public Page<BarcodeMachineGrantVO> like(String str, int current, int size) {
        return barcodeMachineGrantMapper.pageLike(str, new Page<>(current, size));
    }

    @Override
    public boolean remove(String ids) {
        return removeByIds(Arrays.asList(ids.split(",")));
    }

    @Override
    public String viewQRCode(String barcodeMachineNumber, String id) {
//        BarcodeMachineGrant barcodeMachineGrant = barcodeMachineGrantDao.findByBarcodeMachineNumber(number);
        BarcodeMachineGrant barcodeMachineGrant = barcodeMachineGrantMapper.findByBarcodeMachineNumber(barcodeMachineNumber);


        String uuid = UUIDUtils.getUUID20();
        redisUtil.set(uuid, barcodeMachineGrant.getBarcodeMachineNumber(), timeout);
        return uuid;
    }

    @Override
    public BarcodeMachineGrant findByMachineNumberAndGrantDate(String barcodeMachineNumber, String date) {
        return barcodeMachineGrantMapper.findByMachineNumberAndGrantDate(barcodeMachineNumber, "%" + date + "%");
    }

    @Override
    public BarcodeMachineGrantVO findById(String id) {
        return barcodeMachineGrantMapper.findById(id);
    }

    @Override
    public List<BarcodeMachineGrantVO> findAll() {
        return barcodeMachineGrantMapper.findAll();
    }

    @Override
    public List<BarcodeMachineGrantVO> findByLike(String keyWord) {
        return barcodeMachineGrantMapper.findByLike("%" + keyWord + "%");
    }

    @Override
    public List<BarcodeMachineGrantVO> findAllDistinct() {
        return barcodeMachineGrantMapper.findAllDistinct();
    }

    @Override
    public List<BarcodeMachineGrantVO> findDistinctByLike(String keyWord) {
        return barcodeMachineGrantMapper.findDistinctByLike("%" + keyWord + "%");
    }
}
